/**
 * Filename: BannerAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/13 14:10
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.OfficialAlbumListActivity;
import com.github.postsanf.yinian.activity.PuzzleSelectActivity;
import com.github.postsanf.yinian.activity.TimeGoodsShopActivity;
import com.github.postsanf.yinian.activity.YNBannerWebActivity;
import com.github.postsanf.yinian.bean.YNBanner;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.service.TargetService;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Banner适配器
 */
public class BannerAdapter extends PagerAdapter {
    private Activity mContext;
    private List<YNBanner> mAdList = new ArrayList<>();
    private ArrayList<View> mViews = new ArrayList<>();
    private boolean isActivity;

    public BannerAdapter(Activity mContext, List<YNBanner> mAdList) {
        this.mContext = mContext;
        this.mAdList = mAdList;
//		initImgs();
    }

    public List<View> getMViews() {
        return mViews;
    }

    @Override
    public int getCount() {
        return mAdList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == obj;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.yn_item_home_banner, null);
        final SimpleDraweeView iv_item_home_banner = (SimpleDraweeView) view.findViewById(R.id.iv_item_home_banner);
        final YNBanner mBannerItem = mAdList.get(position);
        if (isActivity) {
            int width = UIHelper.getScreenWidth(mContext) - UIHelper.dip2px(mContext, 20);
            int height = 8 * width / 10;
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
            iv_item_home_banner.setLayoutParams(params);
        }
        ImageOptHelper.showFrescoHome(Uri.parse(YNUrlHelper.getHomeImgUrl(mContext, mBannerItem.getBpic())), iv_item_home_banner, mContext);
        // 在这个方法里面设置图片的点击事件
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (mBannerItem.getBtype()) {
                    // 链接
                    case 0:
                        Intent banner_intent = new Intent(mContext, YNBannerWebActivity.class);
                        banner_intent.putExtra(CommonConstants.YNBANNERURL, mBannerItem.getBdata());
                        mContext.startActivity(banner_intent);
                        break;
                    // 官方相册列表
                    case 1:
                        Intent intent = new Intent(mContext, OfficialAlbumListActivity.class);
                        mContext.startActivity(intent);
                        break;
                    // 官方相册
                    case 2:
                        Intent mIntent = new Intent(mContext, TargetService.class);
                        mIntent.setAction(ACTIONs.actionGetTarAlbum);
                        mIntent.putExtra(ACTIONs.aAddData, mBannerItem.getBdata());
                        mContext.startService(mIntent);
                        break;
                    //电商
                    case 3:
//                        mContext.startActivity(new Intent(mContext, TimeGoodsWebActivity.class));
                        Intent shopintent = new Intent(mContext, TimeGoodsShopActivity.class);
                        mContext.startActivity(shopintent);
                        if (listener != null) {
                            listener.onBinnerClickListener();
                        }
                        break;
                    case 5:
                        mContext.startActivity(new Intent(mContext, PuzzleSelectActivity.class));
                        break;
                }
            }
        });
        mViews.add(view);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // 这里不需要做任何事情
    }

    public void setActivity(boolean isActivity) {
        this.isActivity = isActivity;
    }

    //接口回调
    private OnBinnerClickListener listener;

    public interface OnBinnerClickListener {
        void onBinnerClickListener();
    }

    public void setOnBinnerClickListener(OnBinnerClickListener listener) {
        this.listener = listener;
    }
}